﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MBB_Payroll_Terminal
{
    
    public partial class frmMain : Form
    {
        frmDTR dtrfrm = new frmDTR();
        public frmMain()
        {
            InitializeComponent();

            this.Resize += new EventHandler(frmMain_Resize);
            this.FormClosing += new FormClosingEventHandler(frmMain_FormClosing);
            this.Load += new EventHandler(frmMain_Load);
            sldMain.ItemRemoved += new EventHandler(sldMain_ItemRemoved);
            sldMain.ItemAdded += new EventHandler(sldMain_ItemAdded);
            sldMain.AnimationCompleted += new EventHandler(sldMain_AnimationCompleted);
            mnuCloseAll.Click += new EventHandler(mnuCloseAll_Click);

            //Menus
            mnuAbout.Click += new EventHandler(mnuAbout_Click);
            mnuExit.Click += new EventHandler(mnuExit_Click);
            mnuSettings.Click += new EventHandler(mnuSettings_Click);

            mnuIn.Click += new EventHandler(Time_In_Out);
            mnuOut.Click += new EventHandler(Time_In_Out);
            mnuImportData.Click += new EventHandler(mnuImportData_Click);
            mnuExportDTR.Click += new EventHandler(mnuExport_Click);
            mnuExportImages.Click += new EventHandler(mnuExportImages_Click);
            mnuShow.Click += new EventHandler(Time_In_Out);

            MainForm.themain = this;
        }

        void mnuExportImages_Click(object sender, EventArgs e)
        {
            frmPassword pwd = new frmPassword();
            DialogResult dlg = pwd.ShowDialog();
            if (dlg == System.Windows.Forms.DialogResult.OK)
            {
                frmExportImages frm = new frmExportImages();
                frm.Initialize("Export Images");
                sldMain.AddItem(frm.PanelForm, "Export Images");
            }
        }

        void mnuExport_Click(object sender, EventArgs e)
        {
            frmPassword pwd = new frmPassword();
            DialogResult dlg = pwd.ShowDialog();
            if (dlg == System.Windows.Forms.DialogResult.OK)
            {
                frmExportDTR frm = new frmExportDTR();
                frm.Initialize("Export DTR");
                sldMain.AddItem(frm.PanelForm, "Export DTR");
            }
        }

        void mnuImportData_Click(object sender, EventArgs e)
        {
            frmPassword pwd = new frmPassword();
            DialogResult dlg = pwd.ShowDialog();
            if (dlg == System.Windows.Forms.DialogResult.OK)
            {
                frmImportData frm = new frmImportData();
                frm.Initialize("Import Employee Data");
                sldMain.AddItem(frm.PanelForm, "Import Employee Data");
            }
        }

        void Time_In_Out(object sender, EventArgs e)
        {
            ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;
            switch (tsmi.Name)
            {
                case "mnuIn":
                    dtrfrm.SetIn();
                    break;
                case "mnuOut":
                    dtrfrm.SetOut();
                    break;
                case "mnuShow":
                    dtrfrm.SetShow();
                    break;
            }
        }

        void mnuSettings_Click(object sender, EventArgs e)
        {

            bool found = false;
            for (int i = 0; i < sldMain.ItemList.Length; i++)
            {
                if (sldMain.ItemList[i].Equals("Settings")) found = true;
            }
            if (!found)
            {
                frmPassword pwd = new frmPassword();
                DialogResult dlg = pwd.ShowDialog();
                if (dlg == System.Windows.Forms.DialogResult.OK)
                {
                    frmSettings frm = new frmSettings();
                    frm.Initialize("Settings");
                    sldMain.AddItem(frm.PanelForm, "Settings");
                }
            }
        }

        void mnuAbout_Click(object sender, EventArgs e)
        {
            frmAboutThis frm = new frmAboutThis();
            frm.Initialize("About Payroll Manager");
            sldMain.AddItem(frm.PanelForm, "About Payroll Manager");
        }
        void mnuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (sldMain.ItemCount > 0)
            {
                string msg = string.Format("There are {0} session(s) running. Are you sure to close the application?", sldMain.ItemCount);
                DialogResult dlg = MessageBox.Show(msg, "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (dlg == DialogResult.No) e.Cancel = true;
            }
        }
        void frmMain_Resize(object sender, EventArgs e)
        {
            sldMain.Width = this.Width - 40;
            sldMain.Height = this.Height - 77;
        }
        void sldMain_ItemRemoved(object sender, EventArgs e)
        {
            string[] buf = sldMain.ItemList;
            mnuWindow.DropDownItems.Clear();
            for (int i = 0; i < buf.Length; i++)
            {
                mnuWindow.DropDownItems.Add((i + 1) + "-" + buf[i], null, new EventHandler(window_Click));
                mnuWindow.DropDownItems[i].ForeColor = Color.Black;
            }
            if (sldMain.ItemCount > 0) mnuWindow.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
            mnuCloseAll.Text = "Close All Sessions (" + buf.Length + ")";
        }
        void sldMain_ItemAdded(object sender, EventArgs e)
        {
            string[] buf = sldMain.ItemList;
            mnuWindow.DropDownItems.Clear();
            for (int i = 0; i < buf.Length; i++)
            {
                mnuWindow.DropDownItems.Add((i + 1) + "-" + buf[i], null, new EventHandler(window_Click));
                mnuWindow.DropDownItems[i].ForeColor = Color.Black;
            }
            mnuWindow.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
            mnuCloseAll.Text = "Close All Sessions (" + buf.Length + ")";
        }
        bool toreset = false;
        void sldMain_AnimationCompleted(object sender, EventArgs e)
        {
            for (int i = 0; i < mnuWindow.DropDownItems.Count; i++)
            {
                mnuWindow.DropDownItems[i].ForeColor = Color.Black;
            }
            mnuWindow.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
            if (dtrfrm.PanelForm.Enabled)
            {
                if (!toreset)
                {
                    dtrfrm.IDFocus();
                    toreset = true;
                }
            }
            else
            {
                toreset = false;
            }
        }
        void window_Click(object sender, EventArgs e)
        {
            int index = mnuWindow.DropDownItems.IndexOf((ToolStripItem)sender);
            for (int i = 0; i < mnuWindow.DropDownItems.Count; i++)
            {
                mnuWindow.DropDownItems[i].ForeColor = Color.Black;
            }
            sldMain.MoveTo(index);
            mnuWindow.DropDownItems[index].ForeColor = Color.RoyalBlue;
        }
        void mnuCloseAll_Click(object sender, EventArgs e)
        {
            if (sldMain.ItemCount > 0)
            {
                sldMain.Controls.Clear();
            }
        }

        void frmMain_Load(object sender, EventArgs e)
        {
            //Load and apply settings
            frmSettings.LoadSettings();
            if (frmSettings.setPref[0].ToString() == "True") UnityScriptEngine.Dev.Debug = true;
            if (frmSettings.setPref[1].ToString() == "True") this.WindowState = FormWindowState.Maximized;

            
            dtrfrm.Initialize("Daily Time Record");
            sldMain.AddItem(dtrfrm.PanelForm, "Daily Time Record");
            dtrfrm.SetIn();
            mnuData.Enabled = true;
        }
        public void ShowDTR(string id, string name)
        {
            frmShowDTR frm = new frmShowDTR(id);
            frm.Initialize("DTR : "+ name);
            sldMain.AddItem(frm.PanelForm, "DTR : " + name);
        }
    }
    public static class MainForm
    {
        public static frmMain themain;
    }
}
